package main

import (
	"fmt"
	"strconv"
	"time"
)

type Product struct {
	ID   int
	Name string
	Time string
}

type Seller struct {
	ID int
	Product
}

func main1() {
	var productChan = make(chan Product, 10)
	go produce(productChan)
	time.Sleep(time.Second)
	close(productChan)
}
func main() {
	var productChan = make(chan Product, 10)
	var sellerChan = make(chan Seller, 10)
	go produce(productChan)
	go sell(sellerChan, productChan)
	for {
		if len(sellerChan) == 10 {
			break
		}
	}
	close(productChan)
	close(sellerChan)
}

func produce(productChan chan<- Product) {
	for i := 0; i < 10; i++ {
		name := "名字" + strconv.Itoa(i)
		timeStr := time.Now().Format("2006-01-02 15:04:05")
		product := Product{ID: i + 1, Name: name, Time: timeStr}
		fmt.Println(product)
		productChan <- product
		time.Sleep(1 * time.Second)
	}
}

func sell(sellerChan chan<- Seller, productChan <-chan Product) {
	for i := 0; i < 10; i++ {
		id := len(sellerChan) + 1
		seller := Seller{ID: id, Product: <-productChan}
		fmt.Println(seller)
		sellerChan <- seller
	}
}
